package com.sea.onlinejudge.controller;

import com.sea.onlinejudge.common.BaseData;
import com.sea.onlinejudge.common.annotation.Login;
import com.sea.onlinejudge.common.utils.JwtTokenUtil;
import com.sea.onlinejudge.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    TestService testService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @CrossOrigin
    @RequestMapping("/hello")
    public String index() {
        return "Hello, world.";
    }

    @CrossOrigin
    @RequestMapping("/getAllInfo")
    public BaseData getAllInfo() {
        ArrayList<HashMap<String, String> > data = testService.getAllInfo();
        return BaseData.success().setData(data);
    }

    @Login
    @CrossOrigin
    @RequestMapping("/getInfoById")
    public BaseData getInfoById(@RequestBody BaseData rqs) {
        HashMap<String, String> requestData = (HashMap)rqs.getData();
        HashMap<String, String> data = testService.getInfoById(requestData.get("name"));
        return BaseData.success().setData(data);
    }

    @CrossOrigin
    @RequestMapping(method = RequestMethod.GET, value = "/getToken")
    public BaseData getToken() {
        HashMap<String, String> data = new HashMap<>();
        data.put("Token", jwtTokenUtil.builderToken("haha"));
        return BaseData.success().setData(data);
    }
}
